package com.xh.hongaiagent.rag;


import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;


// SpringAi自带内存向量数据库
@Configuration
public class LoveAppVectorStoreConfig {

    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;
    @Resource
    private MyTokenTextSplitter myTockenTextSplitter;
    @Resource
    private MyDocumentEnricher myDocumentEnricher;


    /**
     * 创建springai自带的SimpleVectorStore向量数据库
     * @param dashscopeEmbeddingModel 嵌入模型
     * @return
     */
    @Bean
    VectorStore loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel){
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel).build();
        List<Document> documents = loveAppDocumentLoader.loadMarkdonws();
        //对文档进行切词
        List<Document> splittedDocument = myTockenTextSplitter.splitCustomized(documents);
        //利用ai对文档进行关键词提取，作为元信息
        List<Document> enrichedDocument = myDocumentEnricher.enrichDocumentsByKeyword(splittedDocument);

        simpleVectorStore.add(enrichedDocument);
        return simpleVectorStore;
    }
}
